﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Erp
{
    /// <summary>
    /// 物料库存服务
    /// </summary>
    public class MatlInvService : ErpAppService, IMatlInvService
    {
        private IMatlInvRepository _matlInvRepository;

        public MatlInvService(IMatlInvRepository matlInvRepository)
        {
            _matlInvRepository = matlInvRepository;
        }

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<MatlInvDto>> GetInvRecListAsync(MatlInvInput input)
        {
            var iQuery = await _matlInvRepository.WithDetailsAsync();
            //iQuery = iQuery.Where(i => i.MatlWhseId == input.matlWhseId);
            //iQuery = iQuery.WhereIf(!string.IsNullOrWhiteSpace(input.Filter), i => i.OrdNo.Contains(input.Filter));
            //iQuery = iQuery.WhereIf(input.Begndata != null, i => i.InboundTime.Date >= input.Begndata.Value.Date);
            //iQuery = iQuery.WhereIf(input.Enddata != null, i => i.InboundTime.Date <= input.Enddata.Value.Date);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<MatlInv>, List<MatlInvDto>>(iQuery.ToList());

                return new PagedResultDto<MatlInvDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<MatlInv>, List<MatlInvDto>>(items.ToList());

                return new PagedResultDto<MatlInvDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取库存明细列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<MatlInvDto>> GetInvDetlListAsync(MatlInvInput input)
        {
            var iQuery = await _matlInvRepository.GetInvDetlListAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<MatlInv>, List<MatlInvDto>>(iQuery.ToList());

                return new PagedResultDto<MatlInvDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<MatlInv>, List<MatlInvDto>>(items.ToList());

                return new PagedResultDto<MatlInvDto>(totalCount, itemsDto);
            }
        }

        /// <summary>
        /// 获取库存统计列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<MatlInvDto>> GetInvSumListAsync(MatlInvInput input)
        {
            var list = await _matlInvRepository.GetInvSumListAsync(input.Pi, input.Ps);
            var totalCount = list.Count();
            var itemsDto = ObjectMapper.Map<List<MatlInv>, List<MatlInvDto>>(list);

            return new PagedResultDto<MatlInvDto>(totalCount, itemsDto);
        }
    }
}
